package com.du.stutestsys.serverImpl.AdministratorImpl;

import com.du.stutestsys.dao.GradeMapper;
import com.du.stutestsys.entity.Grade;
import com.du.stutestsys.service.Administrator.GradeService;
import com.du.stutestsys.util.PageUtil;
import com.du.stutestsys.util.DuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class GradeServiceImpl implements GradeService {
    @Autowired
    GradeMapper gradeMapper;
    @Override
    public boolean addGrade(Grade grade) {
        return gradeMapper.addGrade(grade)>0;
    }

    @Override
    public boolean updateGrade(Grade grade) {
        return gradeMapper.updateGrade(grade)>0;
    }

    @Override
    public boolean addGradeSubject(Long grade_id, String[] subject_ids) {
        return gradeMapper.addGradeSubject(grade_id, subject_ids)>0;
    }

    @Override
    public List<Grade> selectGrade(Grade grade, PageUtil pageUtil ) {
        return gradeMapper.selectGrade(grade,pageUtil);
    }

    //一次提交两个增加事务（事务一提交SQL成功，事务二SQL失败），则事务一提交数据回滚
    //一根绳子上的蚂蚱
    //**增加年级and年级科目**
    @Override
    @Transactional
    public boolean addGradeAndSubject(Grade grade, String[] subject_ids) {
        boolean add1 = addGrade(grade);
        boolean add2 = addGradeSubject(grade.getId(), subject_ids);
        return DuUtil.isAdds(new Boolean[]{add1,add2});
    }


}
